﻿namespace Hidistro.SaleSystem.Member
{
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities.Members;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.Membership.Core.Enums;
    using Hidistro.SqlDal.Members;
    using Hidistro.SqlDal.Orders;
    using Hidistro.SqlDal.Sales;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public static class MemberProcessor
    {
        public static bool AddInpourBlance(InpourRequestInfo inpourRequest)
        {
            return new InpourRequestDao().AddInpourBlance(inpourRequest);
        }

        public static int AddShippingAddress(ShippingAddressInfo shippingAddress)
        {
            ShippingAddressDao dao = new ShippingAddressDao();
            return dao.AddShippingAddress(shippingAddress);
        }

        public static bool BalanceDrawRequest(BalanceDrawRequestInfo balanceDrawRequest)
        {
            Globals.EntityCoding(balanceDrawRequest, true);
            bool flag = new BalanceDetailDao().BalanceDrawRequest(balanceDrawRequest);
            if (flag)
            {
                Users.ClearUserCache(HiContext.Current.User);
            }
            return flag;
        }

        public static CreateUserStatus CreateMember(Member member)
        {
            return Users.CreateUser(member, HiContext.Current.Config.RolesConfiguration.Member);
        }

        public static bool DelShippingAddress(int shippingid, int userid)
        {
            return new ShippingAddressDao().DelShippingAddress(shippingid, userid);
        }

        public static DbQueryResult GetBalanceDetails(BalanceDetailQuery query)
        {
            return new BalanceDetailDao().GetBalanceDetails(query);
        }

        public static IList<OpenIdSettingsInfo> GetConfigedItems()
        {
            return new OpenIdSettingDao().GetConfigedItems();
        }

        public static int GetDefaultMemberGrade()
        {
            return new MemberGradeDao().GetDefaultMemberGrade();
        }

        public static ShippingAddressInfo GetDefaultShippingAddress()
        {
            IList<ShippingAddressInfo> shippingAddresses = new ShippingAddressDao().GetShippingAddresses(HiContext.Current.User.UserId);
            foreach (ShippingAddressInfo info in shippingAddresses)
            {
                if (info.IsDefault)
                {
                    return info;
                }
            }
            return null;
        }

        public static InpourRequestInfo GetInpourBlance(string inpourId)
        {
            return new InpourRequestDao().GetInpourBlance(inpourId);
        }

        public static MemberGradeInfo GetMemberGrade(int gradeId)
        {
            return new MemberGradeDao().GetMemberGrade(gradeId);
        }

        public static DbQueryResult GetMyReferralMembers(MemberQuery query)
        {
            return new MemberDao().GetMyReferralMembers(query);
        }

        public static OpenIdSettingsInfo GetOpenIdSettings(string openIdType)
        {
            return new OpenIdSettingDao().GetOpenIdSettings(openIdType);
        }

        public static ShippingAddressInfo GetShippingAddress(int shippingId)
        {
            return new ShippingAddressDao().GetShippingAddress(shippingId);
        }

        public static int GetShippingAddressCount()
        {
            return new ShippingAddressDao().GetShippingAddresses(HiContext.Current.User.UserId).Count;
        }

        public static IList<ShippingAddressInfo> GetShippingAddresses()
        {
            return new ShippingAddressDao().GetShippingAddresses(HiContext.Current.User.UserId);
        }

        public static void GetStatisticsNum(out int noPayOrderNum, out int noReadMessageNum, out int noReplyLeaveCommentNum)
        {
            new MemberDao().GetStatisticsNum(out noPayOrderNum, out noReadMessageNum, out noReplyLeaveCommentNum);
        }

        public static DataSet GetUserOrder(int userId, OrderQuery query)
        {
            return new OrderDao().GetUserOrder(userId, query);
        }

        public static int GetUserOrderCount(int userId, OrderQuery query)
        {
            return new OrderDao().GetUserOrderCount(userId, query);
        }

        public static bool Recharge(BalanceDetailInfo balanceDetails)
        {
            InpourRequestDao dao = new InpourRequestDao();
            bool flag = dao.IsRecharge(balanceDetails.InpourId);
            if (!flag)
            {
                flag = new BalanceDetailDao().InsertBalanceDetail(balanceDetails);
                dao.RemoveInpourRequest(balanceDetails.InpourId);
            }
            return flag;
        }

        public static void RemoveInpourRequest(string inpourId)
        {
            new InpourRequestDao().RemoveInpourRequest(inpourId);
        }

        public static bool SetDefaultShippingAddress(int shippingId, int UserId)
        {
            return new ShippingAddressDao().SetDefaultShippingAddress(shippingId, UserId);
        }

        public static bool UpdateShippingAddress(ShippingAddressInfo shippingAddress)
        {
            return new ShippingAddressDao().UpdateShippingAddress(shippingAddress);
        }

        public static LoginUserStatus ValidLogin(Member member)
        {
            if (member == null)
            {
                return LoginUserStatus.InvalidCredentials;
            }
            return Users.ValidateUser(member);
        }
    }
}

